package com.ruoyi.eqmonitor.dispose.task;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.eqmonitor.dispose.service.IExteriorMonitorDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;

@Slf4j
@Component
@Profile("prod")
public class CreateMonitorTableTask {

    @Resource
    private IExteriorMonitorDataService iExteriorMonitorDataService;

    /**
     * 每月最后一天创建新一个月的分表
     */
//    @Scheduled(cron = "0 0 0 L * ?")
    // 解决Spring定时任务每月最后一天corn使用L报错的问题
    @Scheduled(cron = "0 0 0 28-31 * ?")
    public void analyseTerminalTask() {
        final Calendar c = Calendar.getInstance();
        if (c.get(Calendar.DATE) == c.getActualMaximum(Calendar.DATE)) {
            //是最后一天
            log.info("创建月份表");
            String yyyy_mm = DateUtil.format(DateUtil.offsetMonth(new Date(), 1), "yyyy_MM");
            iExteriorMonitorDataService.createMonthTable(yyyy_mm);
        }
    }
}
